package com.three.iot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.three.iot.api.ProductService;
import com.three.iot.channel.api.ProductChannelService;
import com.three.iot.channel.impl.aliyun.AliyunConstants;
import com.three.iot.channel.po.ProductCondPo;
import com.three.iot.channel.po.ProductRespPo;
import com.three.iot.commons.dto.ProductCondDto;
import com.three.iot.commons.dto.ProductResultDto;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.StatusEnum;
import com.three.iot.commons.utils.DateUtil;
import com.three.iot.commons.utils.IDUtils;
import com.three.iot.jdbc.mapper.TProductInfoMapper;
import com.three.iot.jdbc.model.TProductInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private TProductInfoMapper tProductInfoMapper;
    @Resource
    private ProductChannelService productChannelService;
    @Override
    public List<TProductInfo> findAll() {
        return tProductInfoMapper.listAll();
    }

    @Override
    public void refreshProduct(int pageNum,String userNo) {
        ProductCondPo productCondPo=new ProductCondPo();
        productCondPo.setPageNum(pageNum);
        productCondPo.setPageSize(AliyunConstants.PRODUCT_PAGE_SIZE);
        Result<Page<ProductRespPo>> productRespPage=productChannelService.findProductList(productCondPo);
        if(productRespPage.isSuccess()){
            Page<ProductRespPo> productRespPoPage=productRespPage.getData();
            for(ProductRespPo productRespPo:productRespPoPage.getResult()){
                TProductInfo tProductInfo=new TProductInfo();
                tProductInfo.setProductKey(productRespPo.getProductKey());
                tProductInfo.setProductName(productRespPo.getProductName());
                tProductInfo.setProductDesc(productRespPo.getDescription());
                tProductInfo.setModiNo(userNo);
                saveProduct(tProductInfo);
            }
            if(productRespPoPage.getPages()!=pageNum){
                refreshProduct(pageNum+1,userNo);
            }
        }
    }


    @Override
    public void saveProduct(TProductInfo tProductInfo) {
        TProductInfo oldProduct=tProductInfoMapper.selectByProductKey(tProductInfo.getProductKey());
        if(oldProduct==null){
            tProductInfo.setProductNo(IDUtils.nextIdStr());
            tProductInfo.setCreateNo(tProductInfo.getModiNo());
            tProductInfo.setCreateTime(new Date());
            tProductInfo.setModiTime(new Date());
            tProductInfoMapper.insertSelective(tProductInfo);
        }else{
            tProductInfo.setId(oldProduct.getId());
            tProductInfo.setModiTime(new Date());
            tProductInfoMapper.updateByPrimaryKeySelective(tProductInfo);
        }
    }

    @Override
    public Page<ProductResultDto> listPage(ProductCondDto prodCondDto) {
        //使用PageHelper类进行分页
        PageHelper.startPage(prodCondDto.getPageNum(), prodCondDto.getPageSize());
        Page<TProductInfo> productInfoPage = (Page<TProductInfo>) tProductInfoMapper.selectList(prodCondDto);
        Page<ProductResultDto> productResultDtoPage=new Page<ProductResultDto>();
        BeanUtils.copyProperties(productInfoPage,productResultDtoPage);
        List<TProductInfo> productInfos=productInfoPage.getResult();
        for(TProductInfo productInfo:productInfos){
            ProductResultDto productResultDto=new ProductResultDto();
            BeanUtils.copyProperties(productInfo,productResultDto);
            productResultDto.setStatus(String.valueOf(productInfo.getStatus()));
            productResultDto.setStatusName(StatusEnum.getNameByCode(productInfo.getStatus()));
            productResultDto.setCreateTime(DateUtil.getDateTimeFormat(productInfo.getCreateTime()));
            productResultDto.setModiTime(DateUtil.getDateTimeFormat(productInfo.getModiTime()));
            productResultDtoPage.add(productResultDto);
        }
        return productResultDtoPage;
    }
}
